/**
 * 
 * Copyright (C) 2011 Cody Stoutenburg . All rights reserved.
 *
 *       This program is free software; you can redistribute it and/or
 *       modify it under the terms of the GNU Lesser General Public License
 *       as published by the Free Software Foundation; either version 2.1
 *       of the License, or (at your option) any later version.
 *
 *       This program is distributed in the hope that it will be useful,
 *       but WITHOUT ANY WARRANTY; without even the implied warranty of
 *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *       GNU Lesser General Public License for more details.
 *
 *       You should have received a copy of the GNU Lesser General Public License
 *       along with this program; if not, write to the Free Software
 *       Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
 * 
 */
package ca.usherbrooke.view.slick.jade.worldView;

import java.util.ArrayList;
import java.util.List;

import org.newdawn.slick.Animation;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.geom.Rectangle;

import ca.usherbrooke.model.entity.IEntityModel;
import ca.usherbrooke.model.equipment.EquipmentModel;
import ca.usherbrooke.model.world.IWorldModel;
import ca.usherbrooke.view.slick.jade.EntityViewJade;
import ca.usherbrooke.view.slick.jade.EquipmentViewJade;
import ca.usherbrooke.view.slick.jade.IWorldProvider;
import ca.usherbrooke.view.slick.jade.worldView.shared.EntityViewDebug;

/**
 * @author Cody Stoutenburg
 * 
 */
public class EntityWorldView extends AbstractWorldView {
	private static Color HIDE_COLOR = new Color(0f, 0f, 0f, 0.7f);

	protected final List<EntityViewJade> allEntity;
	protected final List<EquipmentViewJade> allEquipments;

	private final Long entityId;

	/**
	 * @param world
	 */
	public EntityWorldView(IWorldProvider world, Long entityId) {
		super(world);
		allEntity = new ArrayList<EntityViewJade>();
		allEquipments = new ArrayList<EquipmentViewJade>();

		this.entityId = entityId;
	}

	@Override
	public void initWorld() {
		IWorldModel w = world.getWorld();
		if (w == null)
			return;

		allEntity.clear();
		allEquipments.clear();
		List<IEntityModel> entities = w.getAllEntities();
		for (IEntityModel entity : entities) {
			addEntity(entity);
		}
		List<EquipmentModel> equiments = w.getAllEquipments();
		for (EquipmentModel equipment : equiments) {
			addEquipment(equipment);
		}
	}

	@Override
	public void render(GameContainer container, Graphics g) {
		Color orgColor = g.getColor();
		// all debug info are in red
		g.setColor(Color.white);

		for (EntityViewJade entity : allEntity) {
			entity.render(container, g);
		}
		for (EquipmentViewJade eq : allEquipments) {
			eq.render(container, g);
		}

		IWorldModel w = world.getWorld();
		IEntityModel entity = w.getEntity(entityId);

		Color previousColor = g.getColor();

		g.setColor(HIDE_COLOR);
		g.draw(entity.getViewingArea());

		Rectangle view = entity.getViewingArea();

		Rectangle worldSize = w.getSize();
		// hide upper rectangle
		generateRectangle(g, 0, 0, worldSize.getWidth(), view.getMinY());

		// hide lower rectangle
		generateRectangle(g, 0, view.getMaxY(), worldSize.getWidth(),
				worldSize.getHeight());

		// hide left rectangle
		generateRectangle(g, 0, view.getMinY(), view.getMinX(), view.getMaxY());

		// hide right rectangle
		generateRectangle(g, view.getMaxX(), view.getMinY(),
				worldSize.getWidth(), view.getMaxY());

		g.setColor(previousColor);

		EntityViewDebug.INSTANCE.render(entityId, g);
		g.setColor(orgColor);
	}

	private void generateRectangle(Graphics g, float x, float y, float xMax,
			float yMax) {

		if (xMax - x > 0 && yMax - y > 0) {
			Rectangle rect = new Rectangle(x, y, (xMax - x) + 1, (yMax - y) + 1);
			g.fill(rect);
		}
	}

	@Override
	public void update(GameContainer container, int delta) {
	}

	private void addEntity(IEntityModel model) {
		Animation[] allAnimation = getViewer().getEntityProperties()
				.getAnimation(model.getName());
		EntityViewJade view = new EntityViewJade(allAnimation[0],
				allAnimation[1], allAnimation[2], allAnimation[3],
				model.getGuid(), world);
		allEntity.add(view);
	}

	private void addEquipment(EquipmentModel model) {
		Image equipeImage = getViewer().getEquipmentProperties().getImage(
				model.getName());
		EquipmentViewJade equipementView = new EquipmentViewJade(equipeImage,
				model.getGuid(), world);
		allEquipments.add(equipementView);
	}
}
